//车辆评价
//杨帆
interface CarCommentManagent extends ng.IScope {
    commentIf:boolean;
    wilddogList:Array<any>;
    isReadonly:boolean;
    evaluateClass:boolean;
    /**
     * 未评价
     */
    notEvaluation:()=>void;
    /**
     * 已评
     */
    evaluate:()=>void;
    /**
     * 切换未评价
     */
    haveEvaluation:()=>void;
    /**
     * 未评数据
     */
    dataArrList:Array<unEvaluateListItemResponse>;
    /**
     * 已评价
     */
    readEvaluateList:Array<any>;
    /**
     * 未评价
     */
    unReadList:Array<any>;

    // dataArrList:InotEvaluation[];
    /**
     * 未评价数
     */
    notNum:number;
    /**
     * 保存评价
     */
    evaluateSave:(item:InotEvaluation)=>void;
    /**
     * 未评序号
     */
    indexs:number;
}

/**
 * 未评数据
 */
interface InotEvaluation {
    credibility : string;
    timeliness : string;
    attitude : string;
    freight : string;
    content : string;
    index?:number;
    CarrierOrderId:string;
    
}
class CarCommentManagentController{
    wilddogList=[];
    refEvaluat:any;
    appraiserId:string;
    appraiser:string;
    readList=[]
    
    constructor( private $scope:CarCommentManagent,private carService:ICarService,private $location: ng.ILocationService ,private $timeout:ng.ITimeoutService,private orderService:IorderService,private $ngBootbox:BootboxService,
        private commonService:ICommonService){//,private $wilddogArray:any
        this.appraiserId = JSON.parse(localStorage.getItem("loginData")).id;//评价人编号
        this.appraiser = JSON.parse(localStorage.getItem("loginData")).realName;//评论人
        
        this.init();
    }

    init():void{
        this.$scope.max=5;
        this.$scope.totalItems= 5;
        this.$scope.currentPage = 0;
        this.$scope.commentIf = true;
        this.$scope.isReadonly = false;
        this.$scope.evaluateClass = true;
        
        this.$scope.carCode = this.$location.search().carCode;
        this.$scope.evaluate= this.evaluate;
        this.$scope.notEvaluation = this.notEvaluation;

        this.getNotEvaluation();
        // this.load();
        this.$scope.evaluateSave = (item)=>{
            this.evaluateSave(item)
        }
    }

    // load(page?:number):void{
    //     this.readList = [];
    //     this.carService.getRatringList(0,-1).then((res)=>{
    //         if(!page){page =0}else{
    //             page = page -1;
    //         } 

    //         if(res.data.data){
    //             //已评价数据
    //             this.$scope.readEvaluateList = res.data.data.filter(item=>{return item.isRated});
    //             console.log('read',this.$scope.readEvaluateList)  ;
    //             this.readList= this.$scope.readEvaluateList 
    //         }
    //         if(this.readList){
                
    //             this.$scope.total = this.$scope.readEvaluateList.length;

    //             this.readList.forEach(item=>{
    //                 item['integrityStar']=item.integrity/2;
    //                 item['timelinessStar']=item.timeliness/2;
    //                 item['attitudeStar'] = item.attitude/2;
    //                 item['pricingStar'] = item.pricing/2;
    //             })
    //             this.readList.map((item,index)=>{ //.sort('')排序
    //                 item.index = (page*this.$scope.max)+ index+1
    //             });
    //         }
                           
    //         this.$scope.$watch("commentIf",(newVal, oldVal)=>{
    //             if(!this.$scope.commentIf){
    //                 this.$scope.totalItems =  this.$scope.total
    //             }else{
    //                 //this.$scope.totalItems = this.$scope.wilddogList.length;
    //             }
    //         });
    //     });
    // }

    /**
     * 未评价
     */
    notEvaluation=()=>{
        $("#notEvaluation").addClass("Evaluation");
        $("#haveEvaluation").removeClass("Evaluation")
        this.$scope.currentPage = 1;
        this.$scope.commentIf = true;
        this.$scope.isReadonly = false;
    }

    /**
     * 已评价
     */
    evaluate=()=>{
        $("#notEvaluation").removeClass("Evaluation");
        $("#haveEvaluation").addClass("Evaluation");
        this.$scope.currentPage = 1;
        this.$scope.commentIf = false;
        this.$scope.isReadonly = true;
    }

    /**
     * 获取未评价数据
     */
    getNotEvaluation=(page?:number)=>{
        //野狗获取改为后台获取
        //未评价数据
        this.wilddogList = [];
        this.carService.getRatringList(0,-1).then((res)=>{
            // this.$scope.notNum = res.data.total;
            if(!page){page =0}else{
                page = page -1;
            }
            if(res.data.data){// dataArrList
                //未评价
                this.$scope.unReadList = res.data.data.filter(item=>{return !item.isRated})
                console.log('UnRead',this.$scope.unReadList);// dataArrList
                this.wilddogList = this.$scope.unReadList;// dataArrList

                
                //已评价数据
                this.$scope.readEvaluateList = res.data.data.filter(item=>{return item.isRated});
                console.log('read',this.$scope.readEvaluateList) ;
                this.readList= this.$scope.readEvaluateList ;
                
                //未评价
                if(this.wilddogList){
                    this.$scope.notNum = this.$scope.unReadList.length;// dataArrList
                    this.wilddogList.forEach((item, index)=>{
                        // item.credibility= "credibility" + item.invoiceOrderId;
                        // // item.timeliness = "timeliness" + item.invoiceOrderId;
                        // item.attitude = "attitude" + item.invoiceOrderId;
                        // item.pricing = "freight" + item.invoiceOrderId;//credibility
                        // item.ratingContent = "content" + item.invoiceOrderId;
                    });      
                    this.wilddogList.map((item,index)=>{ //.sort('')排序
                        item.index = this.$scope.notNum - (page*this.$scope.max)- index
                    });
                }else{
                    this.$scope.notNum = 0;
                    this.$scope.unReadList=[]
                }

                //已评价
                if(this.readList){
                    this.$scope.total = this.$scope.readEvaluateList.length;
                    this.readList.forEach(item=>{
                        item['integrityStar']=item.integrity/2;
                        item['timelinessStar']=item.timeliness/2;
                        item['attitudeStar'] = item.attitude/2;
                        item['pricingStar'] = item.pricing/2;
                    })
                    this.readList.map((item,index)=>{ //.sort('')排序
                        item.index = (page*this.$scope.max)+ index+1
                    });
                }else{
                    this.$scope.total = 0;
                    this.$scope.readEvaluateList=[]
                }
            }

            this.$scope.$watch("commentIf",(newVal, oldVal)=>{
                if(!this.$scope.commentIf){
                    this.$scope.currentPage=1;
                    this.$scope.totalItems =  this.$scope.total;
                }else{
                    this.$scope.currentPage=1;
                    this.$scope.totalItems = this.$scope.notNum;
                }
                this.getList(0);
                this.$scope.pageChanged = ()=>{
                    var currentPage = (this.$scope.currentPage - 1) * this.$scope.max;
                    this.getList(currentPage);
                }
            });

            // this.getList(0);
            // this.$scope.pageChanged = ()=>{
            //     var currentPage = (this.$scope.currentPage - 1) * this.$scope.max;
            //     this.getList(currentPage);
            // }
            
        });
    }

    //分页
    getList=(currentPage:number)=>{
        this.$scope.currentPage = currentPage / 5 + 1;
        if(this.wilddogList){
            this.$scope.dataArrList = this.wilddogList.slice(currentPage, currentPage + 5);
        }
        if(this.readList){
            this.$scope.readEvaluateList = this.readList.slice(currentPage, currentPage + 5);
        }
    }

    /**
     * 保存评价
     */
    evaluateSave=(item)=>{
        console.log('item',item)
        window.localStorage.setItem('CarrierOrderId',item.invoiceOrderId);
        if (typeof (item.credibility) == "string" || typeof (item.credibility) == "undefind") { item.credibility = 0; };
        if (typeof (item.timeliness) == "string" || typeof (item.timeliness) == "undefind") { item.timeliness = 0; };
        if (typeof (item.attitude) == "string" || typeof (item.attitude) == "undefind") { item.attitude = 0; };
        if (typeof (item.pricing) == "string" || typeof (item.pricing) == "undefind") { item.pricing = 0; };
        var tow = 2;
        var isPraise;
        var average = (item.credibility * tow + item.timeliness * tow + item.attitude * tow + item.pricing * tow) / 4;
        average > 4 ? isPraise = true : isPraise = false;
        this.orderService.addEvaluate(
            item.invoiceOrderId,
            item.contractNumber,
            item.licenseNumber,
            item.driverName,
            item.telephone,
            item.credibility * tow,
            item.timeliness * tow,
            item.attitude * tow,
            item.pricing * tow,
            average,
            $("#"+item.ratingContent).val(),
            isPraise,//isRated
            this.appraiserId,
            this.appraiser
        ).then((res)=>{
            $(".content").val("");
            this.getNotEvaluation();
            this.$ngBootbox.alert("评价成功");
        },rej=>{
            // 
        })
    }
}

angular.module("tms").controller("carCommentController", ["$scope", "carService","$location","$timeout","orderService","$ngBootbox","commonService",CarCommentManagentController]);
 //过滤器
angular.module("tms").filter('numbersZero',()=>{
    return (input)=>{
        var number=null;
        if(isNaN(input)){
            return number = 0;
        }else{
            number=input;
        }
        return number;
    }
})